<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: ServiceController.php 4929 2010-08-25 03:36:50Z jam-gate.vn $
 * @since		2.0.0
 */

class Service_ServiceController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new service
	 * 
	 * @return void
	 */
	public function addAction() 
	{	
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$serviceDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceDao();
		$serviceDao->setDbConnection($conn);
		
		$serviceProductDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceProductDao();
		$serviceProductDao->setDbConnection($conn);
		
		$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
		$productDao->setDbConnection($conn);
		
		$nextId 	= 	$serviceDao->getNextId();
		$this->view->assign("nextId",$nextId);
		
		$service = new stdClass();
		$service->id		 		 = $nextId;
		$service->shop_id 			 = Zend_Auth::getInstance()->getIdentity()->shop->id;
		$service->name_vi_VN = Tomato_Config::getConfig()->get("jam")->service_name_vi_VN;
		$service->name_en_US = Tomato_Config::getConfig()->get("jam")->service_name_en_US;
		$service->time			 	 = 0;
		$service->price				 = 0;
		$service->description_vi_VN  = Tomato_Config::getConfig()->get("jam")->service_description_vi_VN;
		$service->description_en_US  = Tomato_Config::getConfig()->get("jam")->service_description_en_US;
		$service->images 		 	 = "";
		$service->is_card		 	 = 0;
		$service->list_product 		 = "";
		$service->block				 = 0;
		$service->serviceProduct	 = array();
			
		$products   = $productDao->find(null,null,null);					
		$this->view->products  = $products;
		
		$request  = $this->getRequest();
		
		$this->view->assign('lang', $request->getParam('lang'));
		
		if ($request->isPost()) {
			$user 	  = Zend_Auth::getInstance()->getIdentity();
			$service->name_vi_VN = $request->getParam("name_vi_VN",$service->name_vi_VN);
			$service->name_en_US = $request->getParam("name_en_US",$service->name_en_US);
			$service->time	     = $request->getParam("time",$service->time);
			$service->price  	 = $request->getParam("price",$service->price);
			$service->description_vi_VN		 = $request->getParam("description_vi_VN",$service->description_vi_VN);
			$service->description_en_US		 = $request->getParam("description_en_US",$service->description_en_US);
			$service->is_card      = ($request->getParam("is_card",$service->is_card)=="on")?1:0;
			$service->images       = $request->getParam("images",$service->images);
			$service->list_product = $request->getParam("list_product",$service->list_product);
			$id = $serviceDao->add($service);

			// xoa het service_product cu di	
			$serviceProductDao->deleteWithServiceId($service->id);			
			// add product to service
			$serviceProduct = new stdClass();
			if($service->list_product){
				$productAndDose      = explode(',',$service->list_product);
				foreach($productAndDose as $item){
					$abc  				 = explode('-', $item);
					$product_id					 = $abc[0];
					$dose						 = $abc[1]; 
					$serviceProduct->id  		 = null;
					$serviceProduct->service_id  = $service->id;
					$serviceProduct->product_id  = $product_id;
					$serviceProduct->dose_used   = $dose;
					$serviceProductDao->add($serviceProduct);
				}
			}			
			
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('service_add_success'));
			$this->_redirect($this->view->serverUrl() . $this->view->url(array("service_id"=>$service->id), 'service_service_edit'));
		}
		$this->view->assign("service",$service);		
	}
	
	/**
	 * Delete service
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
		$response = 'RESULT_ERROR';
		$request  = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$serviceDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceDao();
			$serviceDao->setDbConnection($conn);
			$service = $serviceDao->getById($id);
			
			if ($service != null) {
				$serviceDao->delete($service);
				$response = 'RESULT_OK';
			}
		}
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Edit service
	 * 
	 * @return void
	 */
	public function editAction() 
	{		  
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$serviceDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceDao();
		$serviceDao->setDbConnection($conn);
		
		$serviceProductDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceProductDao();
		$serviceProductDao->setDbConnection($conn);
		
		$productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('product')->getProductDao();
		$productDao->setDbConnection($conn);
		
		$products   = $productDao->find(null,null,null);
		$arrProductId = array();
			
		$this->view->products  = $products;
		
		$request    = $this->getRequest();
		$service_id = $request->getParam("service_id",null);
		if($service_id){
			$service    		= $serviceDao->getById($service_id);
			$exp 				= array("service_id"=>$service->id);
			$serviceProduct 	= $serviceProductDao->find(null,null,$exp);
			$service->serviceProduct = $serviceProduct;
			$list_product       = "";
			$arr				= array();
			$arrProductIdList   = array();
			if($service->serviceProduct){
				foreach($service->serviceProduct as $item){
					$arr[] = $item->product_id.'-'.$item->dose_used;
					$arrProductIdList[] = $item->product_id;
				}
				$list_product = implode('-',$arr);
			}
			$tmp_products   = array();
			if($products)
			foreach($products as $item){
				if(!in_array($item->id,$arrProductIdList))
					$tmp_products[] = $item;
			}			
			$products = $tmp_products;
			$this->view->products  = $products;
			$service->list_product   = $list_product;
						 
			$this->view->assign("serviceProduct",$serviceProduct);
			$this->view->assign('lang', $request->getParam('lang'));			
			if ($request->isPost()) {
				$user 	  = Zend_Auth::getInstance()->getIdentity();
				$service->name_vi_VN = $request->getParam("name_vi_VN",$service->name_vi_VN);
				$service->name_en_US = $request->getParam("name_en_US",$service->name_en_US);
				$service->time	     = $request->getParam("time",$service->time);
				$service->description_vi_VN		 = $request->getParam("description_vi_VN",$service->description_vi_VN);
				$service->description_en_US		 = $request->getParam("description_en_US",$service->description_en_US);
				$service->is_card    = ($request->getParam("is_card",$service->is_card)=="on")?1:0;
				$service->images     = $request->getParam("images",$service->images);
				if($service->block<1){
					$service->price  	 = $request->getParam("price",$service->price);
					$service->list_product = $request->getParam("list_product",$service->list_product);
				
					// xoa het service_product cu di	
					$serviceProductDao->deleteWithServiceId($service->id);				
					// add product to service
					$serviceProduct = new stdClass();
					if($service->list_product){
						$productAndDose      = explode(',',$service->list_product);
						foreach($productAndDose as $item){
							$abc  				 = explode('-', $item);
							$product_id					 = $abc[0];
							$dose						 = $abc[1]; 
							$serviceProduct->id  		 = null;
							$serviceProduct->service_id  = $service->id;
							$serviceProduct->product_id  = $product_id;
							$serviceProduct->dose_used   = $dose;					
							$serviceProductDao->add($serviceProduct);
						}
					}			
				}				
				$id = $serviceDao->update($service);			
				$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translator('service_edit_success'));
				$this->_redirect($this->view->serverUrl() . $this->view->url(array("service_id"=>$service->id), 'service_service_edit'));
			}
			$this->view->assign("service",$service);
		}	
	}
	
	/**
	 * List categories
	 * 
	 * @return void
	 */
	public function listAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$serviceDao = Tomato_Model_Dao_Factory::getInstance()->setModule('service')->getServiceDao();
		$serviceDao->setDbConnection($conn);
		
		$lang = $this->getRequest()->getParam('lang', Tomato_Config::getConfig()->web->lang);
		
		$serviceDao->setLang($lang);
        $request   = $this->getRequest();
		$pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = Tomato_Config::getConfig()->manager->itemPerPage;;
        $offset    = ($pageIndex - 1) * $perPage;
        $exp = array(
        	   );
		$services 	 = $serviceDao->find($offset, $perPage, $exp);
		$this->view->assign('services', $services);
		
		$total       = $serviceDao->count($exp);        
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($services, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        
		$this->view->assign('paginator', $paginator);
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url(array(), 'service_service_list'),
            'itemLink' 	  => 'page-%d'                          
        ));
        $this->view->assign('lang',$lang);				
	} 		
}	